<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>Graduate School Application - Provide Degree</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link rel="stylesheet" type="text/css" href="../resource/columnCSS.css" /> 
	<link rel="stylesheet" type="text/css" href="../resource/styleCSS.css" /> 	
</head>
<body>
	<%  
		// Get incoming parameter from previous page
		String univ_loc = (String)request.getParameter("state");
	
		// Get Vector of universities
		// UPDATE: make it so that we extract from database rather than from support
 		/*support s = new support(); 		
	 	String path2 = config.getServletContext().getRealPath("universities.txt");
		Vector tuples = s.getUniversities(path2);
		Vector universities, tuple = (Vector)tuples.get(0);*/
		Vector universities = new Vector();
		
		try {
			//Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			System.err.println("ClassNotFoundException: " + e);
		} 
		try {	
			//Open a connection to the database
			Connection conn = DriverManager.getConnection(
					"jdbc:postgresql://localhost:5432/CSE135_SP11?"
					+ "user=postgres&password=postgres");
	
			PreparedStatement pstmt = conn.prepareStatement(
					"SELECT id FROM Locations WHERE location = ?");
			
			pstmt.setString(1, univ_loc);
			
			ResultSet rset = pstmt.executeQuery();
			//getting id value from location table
			int location_id = 0;
			while(rset.next())
				location_id = rset.getInt(1);
			
			pstmt = conn.prepareStatement(
					"SELECT university FROM Universities WHERE location = ?");
			pstmt.setInt(1, location_id);
			rset = pstmt.executeQuery();
			
			while(rset.next())
				universities.add(rset.getString("university"));
			
			rset.close();
			pstmt.close();
			conn.close();
		
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
			
			Vector degreeInfo = (Vector) session.getAttribute("degreeInfo");
			
			if ( degreeInfo != null && degreeInfo.size() < 1 ) {
				degreeInfo.add(univ_loc);
				session.setAttribute("degreeInfo", degreeInfo);
			}
			else {
				System.err.println("[DEBUG] Unable to add degree location " +
						"to degree Info");
			}
		
			// Get session information
			String fname = (String)session.getAttribute("fname");
			String lname = (String)session.getAttribute("lname");
			String mname = (String)session.getAttribute("mname");
			
			String citizen = (String)session.getAttribute("citizen");
			String residence = (String)session.getAttribute("residence");
			
			String address = (String)session.getAttribute("address"); 
			String city = (String)session.getAttribute("city");
			String state = (String)session.getAttribute("state");
			Integer zipcode = (Integer)session.getAttribute("zipcode");
			Integer areacode = (Integer)session.getAttribute("areacode");
			Integer number = (Integer)session.getAttribute("number");
			Integer countrycode = (Integer)session.getAttribute("countrycode");
			Boolean domestic = (Boolean)session.getAttribute("domestic");	
	%>
	<p class="sansserif">
	<div class="container">
	<div class="center_div">
		<div class="colmask threecol">
		<div class="colmid">
		<div class="colleft">
			<div class="bodycol">
				<p>Applicant: <i><%= fname%> <%= mname%> <%= lname%></i></p> 
				<p>Citizenship: <i><%= citizen%></i></p>
				<p>Country of Residence: <i><%= residence%></i></p> 
				<p>Address: <i><%= address%></i></p>
				<p>City: <i><%= city%></i> 
				<%
					if (state != null) {
						out.println("State: <i>" + state + "</i>");
					}
				%>
				Zip Code: <i><%= zipcode%></i></p>
				<p>Telephone: <i><%= areacode %> <%= number%></i>
				<%
					if (countrycode != null && countrycode != -1) {
						out.println("Country Code: <i>" + countrycode + "</i>");
					}
				%>
				</p>
				<p>Domestic: <i><%= domestic%> </i> </p>
				<br>
			</div>
			<div class="sidecol">
				<p><a href="../invalidate.jsp">Logout</a></p>
			</div>
		</div>
		</div>
		</div>
		<div style="clear:both;"></div>
		
		<p><b>Provide Degree - Select University</b> </p>
		<form method="GET" action="degreesChooseDisciplineJSP.jsp">
		<ul class="none">
			<li>If your university is not found enter it here:
			<input type="text" name="university"/> 
		<input type="submit" value="Submit"/></li>
		</ul>
		</form>
		
		<div class="colmask threecol">
		<div class="colmid">
		<div class="colleft">
			<div class="col1">	
				<% 
					String url = request.getRequestURI();
					int len = url.length();
					url = url.substring(0, len - 30);
					url = url.concat("degreesChooseDisciplineJSP.jsp?university=");
				
					for (int index = 0; index < universities.size(); index+=3) {						
						out.print("<p> <a href=\"" + url + universities.get(index) + "\">");
						out.print(universities.get(index) + "</a></p>");
					}
				%>
			</div>
			<div class="col2">
				<% 
					for (int index = 1; index < universities.size(); index+=3) {
						out.print("<p> <a href=\"" + url + universities.get(index) + "\">");
						out.print(universities.get(index) + "</a></p>");
					}
				%>
			</div>
			<div class="col3">
				<% 
					for (int index = 2; index < universities.size(); index+=3) {
						out.print("<p> <a href=\"" + url + universities.get(index) + "\">");
						out.print(universities.get(index) + "</a></p>");
					}
				%>

			</div>
		</div>
		</div>
		</div>
	<div style="clear:both;"></div>
	</div>
	</div>
</body>
</html>